﻿Public Class frmContract

    Private Sub frmContract_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CalDeadlineContract()
        getHeaderGrid_Contract()
        reset()
    End Sub

    Protected Sub getHeaderGrid_Contract()
        Dim HeaderText As String() = {"លេខរៀង", "ID", "ឈ្មោះក្រុមហ៊ុន", "ឈ្មោះមេធាវី", "តម្លៃសេវាកម្មក្នុងមួយខែ", "រយៈពេល (ខែ)", "ថ្ងៃចាប់ផ្តើមកុងត្រា", "ថ្ងៃបញ្ចប់កុងត្រា"}
        Dim HeaderWidth As Integer() = {100, 100, 350, 200, 250, 150, 150, 150}
        Dim i As Integer
        dgvData.ColumnCount = HeaderText.Count
        For i = 0 To HeaderText.Count - 1
            dgvData.Columns(i).HeaderText = HeaderText(i)
            dgvData.Columns(i).Width = HeaderWidth(i)
        Next
        dgvData.Columns(1).Visible = False
        dgvData.Columns(6).DefaultCellStyle.Format = "dd/MM/yyyy"
        dgvData.Columns(7).DefaultCellStyle.Format = "dd/MM/yyyy"
    End Sub

    Public Sub getContract_All()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_All(frmMain._LawyerID)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_NotFinish()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_NotFinish(frmMain._LawyerID)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_Finish()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_Finish(frmMain._LawyerID)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub reset()
        txtSearch.Clear()
        rdContractIsNotFinish.Checked = True
        rdContractIsFinish.Checked = False
        rdContractAll.Checked = False
        txtSearch.Focus()
    End Sub

    Protected Sub clear()
        txtSearch.Clear()
        txtSearch.Focus()
    End Sub

    Protected Sub CalDeadlineContract()
        Dim dt As New DataTable
        Dim oContract As New cContract
        dt = oContract.select_Contract_All(frmMain._LawyerID)
        Dim i As Integer
        Dim isDeleted As Boolean = False
        For i = 0 To dt.Rows.Count - 1
            If dt.Rows(i)("IsFinish") = False Then
                If dt.Rows(i)("EndDate") < Format(Now, "yyyy/MM/dd") Then
                    oContract.update_Contract_Finish(dt.Rows(i)("ContractID"))
                    isDeleted = True
                End If
            End If
        Next
        If isDeleted Then
            MessageBox.Show("កុងត្រាមួយចំនួនត្រូវបានបញ្ចប់ហើយ!", "បញ្ចប់កុងត្រា", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub ShowFormWithoutClose(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        frm.Show()
        frm.BringToFront()
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        ShowFormWithoutClose(frmContractEntry)
    End Sub

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        If dgvData.Rows.Count > 0 Then
            frmContractEntry._ContractID = dgvData.CurrentRow.Cells(1).Value
            ShowFormWithoutClose(frmContractEntry)
        Else
            MessageBox.Show("គ្មានពត៌មានសម្រាប់់បង្ហាញទេ!", "បង្ហាញពត៌មាន", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub dgvData_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvData.DoubleClick
        btnShow_Click(Nothing, Nothing)
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        Select_Radio_Search()
    End Sub

    Protected Sub Select_Radio_Search()
        If rdContractIsNotFinish.Checked Then
            getContract_NotFinish_byKeyword()
        ElseIf rdContractIsFinish.Checked Then
            getContract_Finish_byKeyword()
        Else
            getContract_All_byKeyword()
        End If
    End Sub

    Public Sub getContract_All_byKeyword()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_All_byKeyword(frmMain._LawyerID, txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_NotFinish_byKeyword()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_NotFinish_byKeyword(frmMain._LawyerID, txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Public Sub getContract_Finish_byKeyword()
        dgvData.Rows.Clear()
        Dim oContract As New cContract
        Dim dt As New DataTable
        Dim dr As DataRow
        Dim i As Integer = 1

        dt = oContract.select_Contract_Finish_byKeyword(frmMain._LawyerID, txtSearch.Text)
        For Each dr In dt.Rows
            dgvData.Rows.Add(i, dr(0), dr(9), dr(8), dr(2), dr(3), dr(4), dr(5))
            i += 1
        Next
    End Sub

    Private Sub rdContractIsNotFinish_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdContractIsNotFinish.CheckedChanged
        SelectRadio()
    End Sub

    Private Sub rdContractIsFinish_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdContractIsFinish.CheckedChanged
        SelectRadio()
    End Sub

    Private Sub rdContractAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdContractAll.CheckedChanged
        SelectRadio()
    End Sub

    Protected Sub SelectRadio()
        If rdContractIsNotFinish.Checked Then
            getContract_NotFinish()
        ElseIf rdContractIsFinish.Checked Then
            getContract_Finish()
        Else
            getContract_All()
        End If
        clear()
    End Sub
End Class